package com.zou.service.impl;

import com.zou.commons.domain.ReturnObject;
import com.zou.mapper.MinMenuMapper;
import com.zou.pojo.MaxMenu;
import com.zou.pojo.MinMenu;
import com.zou.service.MinMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service("minMenuService")
@Transactional
public class MinMenuServiceImpl implements MinMenuService {
    @Autowired
    private MinMenuMapper minMenuMapper;

    @Override
    public List<MinMenu> querySomeMinMenuByMinMenu(MinMenu minMenu) {
        return minMenuMapper.selectSomeMinMenuByMinMenu(minMenu);
    }

    @Override
    public boolean queryIsExistMinName(String minName) {
        // 为空说明不存在
        MinMenu minMenu = minMenuMapper.selectIsExistMinName(minName);
        if (minMenu == null) {
            return false;
        }
        return true;
    }

    @Override
    public ReturnObject addMinMenu(MinMenu minMenu) {
        ReturnObject returnObject = new ReturnObject();
        if (queryIsExistMinName(minMenu.getMinName())) {
            returnObject.setFail("此名字已存在");
        } else {
            try {
                if (minMenuMapper.insertMinMenu(minMenu) > 0) {
                    returnObject.setSuccess();
                } else {
                    returnObject.setFail("系统忙，请稍后重试");
                }
            } catch (Exception e) {
                e.printStackTrace();
                returnObject.setFail("系统忙，请稍后重试");
            }
        }
        return returnObject;
    }

    @Override
    public ReturnObject delMinMenuById(Integer id) {
        ReturnObject returnObject = new ReturnObject();
        try {
            if (minMenuMapper.deleteMinMenu(id) > 0) {
                returnObject.setSuccess();
            } else {
                returnObject.setFail("系统忙，请稍后重试");
            }
        } catch (Exception e) {
            e.printStackTrace();
            returnObject.setFail("系统忙，请稍后重试");
        }
        return returnObject;
    }

    @Override
    public ReturnObject editMinMenu(MinMenu minMenu) {
        ReturnObject returnObject = new ReturnObject();
        if (queryIsExistMinName(minMenu.getMinName())) {
            returnObject.setFail("此名字已存在");
        } else {
            try {
                if (minMenuMapper.updateMinMenu(minMenu) > 0) {
                    returnObject.setSuccess();
                } else {
                    returnObject.setFail("系统忙，请稍后重试");
                }
            } catch (Exception e) {
                e.printStackTrace();
                returnObject.setFail("系统忙，请稍后重试");
            }
        }
        return returnObject;
    }
}
